package co.fitstart.mobile.dao.support;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.demo2do.core.persistence.GenericDaoSupport;

import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.StudentTargetData;
import co.fitstart.entity.user.User;

/**
 *
 * @author Ark
 *
 */
@Repository("studentDaoSupport")
public class StudentDaoSupport {

    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /**
     * @param student
     * @param courseRound
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<StudentTargetData> listStudentTargetData(Student student, CourseRound courseRound) {
        
        String sentence = "FROM StudentTargetData WHERE disabled = false AND student = :student AND courseRound = :courseRound ORDER BY createTime DESC";
        
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("student", student);
        parameters.put("courseRound", courseRound);

        return genericDaoSupport.searchForList(sentence, parameters);
        
    }
    
    /**
     * @param student
     * @param courseRound
     * @return
     */
    public int countStudentTargetData(Student student, CourseRound courseRound) {

        String sentence = "SELECT COUNT(1) FROM StudentTargetData WHERE disabled = false AND student = :student AND courseRound = :courseRound";
        
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("student", student);
        parameters.put("courseRound", courseRound);

        return genericDaoSupport.searchForInt(sentence, parameters);
        
    }
    
    /**
     * @param user
     * @return
     */
    @SuppressWarnings("rawtypes")
    public Student load(User user){

        String sentence = "FROM Student Where disabled = false AND user = :user ";

        Map<String, Object> parameters = new HashMap<String, Object>();
        
        parameters.put("user", user);

        List list = genericDaoSupport.searchForList(sentence, parameters);
        
        if (list.size() > 0) {
            return (Student) list.get(0);
        }
        return null;
    }
}
